﻿#include "Bullet.h"
#include "ResourceManager.h"
#include <d3dx9.h>

Bullet::Bullet(float x, float y, bool isLeft)
{
	_sprite = new Sprite(_spriteHandler, ResourceManager::GetInstance()->GetTexture(ResourceManager::GetInstance()->TextureName.Bullet),0, 0);

	_sprite->SetFrame(0,4);	

	_xPre = x;
	_yPre = y;

	_x = x;
	_y = y;

	_width = _sprite->getTexture()->getWidth();
	_height = _sprite->getTexture()->getHeight();

	if (isLeft == true)
	{
		_vx = - 0.9;
	} 
	else
	{
		_vx =  0.9;
	}

	_vy = 0;
	_accelY = 0;

	_isGravity = true;
	_state = IS_MOVING;

	_timeExplosion = 0;
	_timeLive = 0;
}

Bullet::~Bullet()
{
	if (_sprite != NULL)
	{
		delete _sprite;
	}
}

void Bullet::Render(int cameraX, int cameraY)
{
	if (_state != IS_REMOVE)
	{
		_sprite->Render(_x, _y, cameraX, cameraY);
	}

}

OBJECT_TYPE Bullet::getObjectType()
{

	return OBJECT_ITEM;
}


void Bullet::Update(list<Object*>* _list,float time)
{
	switch(_state)
	{
	case IS_IDLE:
		break;

	case IS_ACTIVING:
		_x += _vx * time;
		_y += _vy*time + 1/2 * _accelY * time * time;
		if(_y < this->_yPre - 32-5)
			changeState(IS_MOVING);
		break;

	case IS_MOVING:
		Move(_list, time);
		_timeLive += time;
		if (_timeLive > 4000)
		{
			_timeLive = 0;
			changeState(IS_REMOVE);
		}
		break;

	case IS_EXPLOSION:
		_timeExplosion += time;
		if (_timeExplosion > 200)
		{
			_timeExplosion = 0;
			changeState(IS_REMOVE);
		}

		break;
	}

	_sprite->Update(time);
}



void Bullet::Move(list<Object*>* _list, float time)
{
	Object* obj;
	list<Object*>::iterator i;

	//Theo phương x
	_x += _vx*time;	
	//Theo phương y
	_accelY = BULLET_GRAVITY;
	_vy -= _accelY * time;
	_y += _vy * time + 1.0/2 * _accelY * time * time;

	for (i = _list->begin(); i != _list->end(); i++)
	{
		obj = *i;
		Box box(this->_xPre,
			this->_yPre - this->_height, 
			_width, 
			_height, 
			_vx * time, 
			_vy * time + 1.0f/2.0f * _accelY * time * time);

		Box block(obj->getPosX(),
			obj->getPosY() - obj->getHeight(),// - 0.11,
			obj->getWidth(),
			obj->getHeight()
			);

		Box boxphase = Collision::_GetSweptBroadphaseBox(box);
		if (Collision::_AABBCheck(boxphase, block))
		{
			float normalx = 0;
			float normaly = 0;
			float collision_time = Collision::_SweptAABB(box, block, normalx, normaly);
			if (0 < collision_time && collision_time < 1)
			{

				switch(obj->getIDObjType())
				{
				case 9:	//goomba
					if (obj->getObjState() == ES_ACTIVING)
					{
						obj->changeState(ES_FALL);
						this->changeState(IS_EXPLOSION);
					}
					break;

				case 12:	//turtle
					if (obj->getObjState() == ES_ACTIVING || obj->getObjState() == ES_STOP ||obj->getObjState()==ES_MOVE_SHELL_LEFT ||obj->getObjState()==ES_MOVE_SHELL_RIGHT)
					{
						obj->changeState(ES_FALLBULLET);
						this->changeState(IS_EXPLOSION);
					}
					break;
				case 11:	//Piranha, cay an thit
					if (obj->getObjState() != IS_REMOVE || obj->getObjState() != IS_IDLE)
					{
						obj->changeState(IS_REMOVE);
						this->changeState(IS_EXPLOSION);
					}
					break;
				}

				if (normalx == 1 && normaly == 0)		//va cham tu phai -> trai
				{
					_x = _xPre + _vx * collision_time;
					_accelX = 0;
					_vx = 0;//-_vx;

					changeState(IS_EXPLOSION); 
				} 
				else if (normalx == -1 && normaly == 0)		//va cham tu trai -> phai
				{
					_x = _xPre + _vx * collision_time;
					_accelX = 0;
					_vx = 0;//

					changeState(IS_EXPLOSION);
				}
				else if (normalx == 0 && normaly == -1)		//va cham tu duoi -> tren
				{
					_y = _yPre + _vy * collision_time + 1.0f/2.0f * _accelY * collision_time *collision_time;
					_vy = -_vy;
					_accelY = 0;
				}
				else if (normalx == 0 && normaly == 1)		//va cham tu tren -> duoi
				{


					_y = _yPre + _vy * collision_time + 1.0f/2.0f * _accelY * collision_time *collision_time + 0.5;
					_accelY = 0;
					_vy = 0.8;


				}
			}

		}
	}

	_xPre = _x;
	_yPre = _y;
}

void Bullet::changeState(int state)
{
	_state = state;
	switch(state)
	{
	case IS_ACTIVING:
		_vy = 4.00;
		break;

	case IS_MOVING:
		_vy = 0;
		_vx = 0.100;
		break;

	case IS_EXPLOSION:
		_sprite->SetFrame(5,8);
		break;

	case IS_REMOVE:
		break;
	}
}